Ccay.Module.define("ccay.security.role.list",function(page,$S){
	//igrid对象
	var igridObj;
	//授权
	page.rolePermission = function(value, cell) {
		Ccay.Common.openPage("ccay.security.grant.rolePermission",
				"ccay.security.role.grantOperation2Role", {
					tab : '#listProgramTab'
				}, function() {
					ccay.security.role.grantOperation2Role.init(value);
				});
	};
	
	var gridOptions={
		wrap: true,
		editMode: false,
		percentWidth:true,
		showSeq:false,
		storageOp:{
			// 设置igrid缓存数据
			cache: true,
			// 设置缓存key值
			cacheKey: ["name"] 
		},	
		columns:[
			 {freezeable:true, columnType:'operate', content : "${#ccay.security.role.list.renderEdit(data)}",width:"10%"},
		     {field:'programName',header: "ccay.security.label.datascope",align:"left", rule: {required: true},width:"30%"},
		     {field:'description',header: "ccay.program.label.description",align:"left", width:"60%"}
		],
		ajax:{
			type:'POST',
			url: 'services/manage/security/programManage/findPagedPrograms/page/${pageSize}/${curPage}'
	    },
	    fnBeforeRequest : function(opt){
	    	opt.ajax.data.dimensions = Program.getDimensionJson("programItemSearchId");
	    },
		operation:{
			search:{
				btn:'#btnSearch',
				panel:'#queryProgramForm'
			}
		}
	  }
	 //渲染操作按钮
	page.renderEdit = function(record){
		//系统自动生成的Program不允许修改
		if(record.programId === 0){
			return "";
		}
		var str = new Array();
		str.push(Ccay.UI.button("ccay.common.button.edit", "ccay.security.role.list.openEditPage('{0}')".formatURI(record.programId), "", "img", "edit"));
		str.push(Ccay.UI.button("ccay.common.button.view", "ccay.security.role.list.openViewPage('{0}')".formatURI(record.programId), "", "img", "view"));
		str.push(Ccay.UI.button("ccay.common.button.delete", "ccay.security.role.list.toDelete('{0}')".formatURI(record.programId), "", "img", "del"));
		return str.join("");
	};
	
	//新增或修改页面
	page.openEditPage = function(programId){
		Ccay.Page.register("ccay.security.program.edit","manage/security/program/programEdit.html");
		Ccay.Common.openPage("ccay.security.label.datascope",
				"ccay.security.program.edit", {
				 tab : '#listProgramTab'
		}, function() {
			ccay.security.program.edit.init(programId);
		});
	}
	//查看页面
	page.openViewPage = function(programId){
		Ccay.Page.register("ccay.security.program.view","manage/security/program/programView.html");
		Ccay.Common.openPage("ccay.security.label.datascope",
				"ccay.security.program.view", {
				 tab : '#listProgramTab'
		}, function() {
			ccay.security.program.view.init(programId);
		});
	}
	
	//删除前校验
	page.toDelete = function(programId){
		Ccay.doGet({
		 			async:false,
		 			url:"services/manage/security/programManage/checkReferenced/"+programId,
		 			data : "",
		 			success:function(data){
		 				if(data){
		 					Ccay.UI.confirm("数据范围 已被引用，删除时将同时删除其关联的权限，是否继续删除？", function(bool) {
							 	if(bool){
							 		page.deleteProgramCascade(programId);
							 	}
							});
		 				} else {
		 					page.deleteProgramCascade(programId);
		 				}
		 			}			
		 		});
	}
	//执行删除
	page.deleteProgramCascade = function(programId){
		Ccay.doDelete({
 			async:false,
 			url:"services/manage/security/programManage/deleteProgramCascade/"+programId,
 			data : "",
 			success:function(data){
 				$S("#btnSearch").click();
 			}			
 		});
	}
	
	page.ready=function(){
		$S("#listProgramTab").ccayTab();
		Program.renderItems("programItemSearchId","search");
		$S("#programGrid").igrid(gridOptions);	
		
		
		igridObj = $S("#programGrid").manager();
	};
	
	page.closeTab = function(isReload) {
		var listRoleTabLength = $('#listProgramTab').length;
		if(listRoleTabLength){
			$('#listProgramTab').ccayTabRemoveItem();
			if (isReload){
				igridObj.reload(true,true,true);
			}
		}
	};

});